package com.coocaa.x.test;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

public class MyClass {

    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        BigInteger bigInt = new BigInteger(1, digest.digest());
        String ret = bigInt.toString(16);
        int contact = 32 - ret.length();
        if (contact > 0) {
            StringBuffer s = new StringBuffer();
            for (int i = 0; i < contact; i++)
                s.append("0");
            s.append(ret);
            ret = s.toString();
        }
        return ret;
    }

    private static int TO_DOWNLOAD = 1 << 0;
    private static int FILE_NOT_EXIST = 1 << 1;
    private static int FILE_PREPARED = 1 << 2;

    private static int performDownload(int current_vc, int downloaded_vc, int versionCode) {
        if (downloaded_vc == -1) {
//            kernel.onUpdateFileNotExist();
            return TO_DOWNLOAD | FILE_NOT_EXIST;
//            downloader.start(new File(update_file_path), kernel.getDownloadUrl(), info.md5);
        } else if (downloaded_vc == -2) {
//            kernel.onUpdateFileNotExist();
            if (current_vc < versionCode)
                return TO_DOWNLOAD | FILE_NOT_EXIST;
            return FILE_NOT_EXIST;
//                downloader.start(new File(update_file_path), kernel.getDownloadUrl(), info.md5);
        } else {
            if (current_vc >= downloaded_vc && downloaded_vc > 0) {
//                kernel.onUpdateFileNotExist();
                if (current_vc < versionCode)
                    return TO_DOWNLOAD | FILE_NOT_EXIST;
                return FILE_NOT_EXIST;
//                    downloader.start(new File(update_file_path), kernel.getDownloadUrl(), info.md5);
            } else if (downloaded_vc >= 0 && downloaded_vc > current_vc && downloaded_vc >= versionCode) {
//                kernel.onUpdateFilePrepared(update_file_path);
                return FILE_PREPARED;
            } else {
//                kernel.onUpdateFileNotExist();
                if (current_vc < versionCode)
                    return TO_DOWNLOAD | FILE_NOT_EXIST;
                return FILE_NOT_EXIST;
//                    downloader.start(new File(update_file_path), kernel.getDownloadUrl(), info.md5);
            }
        }
    }

    public static void main(String[] args) {
        v(performDownload(1, -1, 2));
        v(performDownload(2, -1, 1));
        v(performDownload(1, -1, 1));

        v(performDownload(1, -2, 2));
        v(performDownload(2, -2, 1));
        v(performDownload(1, -2, 1));

        v(performDownload(1, 2, 2));
        v(performDownload(3, 2, 2));

        v(performDownload(1, 2, 3));
        v(performDownload(2, 2, 3));
        v(performDownload(3, 2, 3));
        v(performDownload(4, 2, 3));

        v(performDownload(1, 3, 2));
        v(performDownload(2, 3, 2));
        v(performDownload(3, 3, 2));
//        System.out.println(getFileMD5(new File("/home/lu/workspace/skyworth/release/AppStore-Coocaa-release2.apk")));
    }

    private static void v(int v) {
        System.out.println("TO_DOWNLOAD:" + ((v & TO_DOWNLOAD) > 0) + "  FILE_NOT_EXIST:" + ((v & FILE_NOT_EXIST) > 0) + "  FILE_PREPARED:" + ((v & FILE_PREPARED) > 0));
    }
}
